package custom.cards;

import java.util.ArrayList;

import com.google.android.gcm.GCMBaseIntentService;

import custom.cards.GameRequest.RequestType;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class GCMIntentService extends GCMBaseIntentService
{
	public final static ArrayList<Handler> HandlerList = new ArrayList<Handler>();


	public GCMIntentService(String senderId)
	{
		super(senderId);
		Log.d("GCMIntentService", senderId);
	}
	
	public GCMIntentService() 
	{
        super("GCMIntentService");
    }

	@Override
	protected void onError(Context arg0, String arg1)
	{
		Log.d("onError", arg1);
	}

	@Override
	protected boolean onRecoverableError(Context context, String errorId)
	{
		Log.d("onRecoverableError", errorId);
		return false;
	}

	@Override
	protected void onMessage(Context arg0, Intent arg1)
	{
		try
		{
			Log.d("onMessage Type:", arg1.getStringExtra("ResponseType"));

			
		/*	for(Handler h : HandlerList)
			{
				if(h != null)
				{
					Message msg = new Message();
					msg.obj = arg1;
					msg.setTarget(h);
					msg.sendToTarget();
				}
			}*/
			
			if(MainActivity.mHandler != null)
			{
				Message msg = new Message();
				msg.obj = arg1;
				msg.setTarget(MainActivity.mHandler);
				msg.sendToTarget();
			}
			
			if(GamePlayActivity.mHandler != null)
			{
				Message msg = new Message();
				msg.obj = arg1;
				msg.setTarget(GamePlayActivity.mHandler);
				msg.sendToTarget();
			}
		}
		catch(Exception ex)
		{
			Log.e("Exception:", "onMessage",ex);
		}
		
		
		
		
	}

	@Override
	protected void onRegistered(Context arg0, String registrationID)
	{
		Log.d("onRegistered", registrationID);
		
		GamePlayActivity.playerID = registrationID;
		GameRequest req = new GameRequest(registrationID);
		new NetworkTask(req).execute();
	}

	@Override
	protected void onUnregistered(Context arg0, String arg1)
	{
		Log.d("onUnregistered", arg1);
	}
}